package io.byte_stream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
第三种拷贝：
    定义小数组
 */
public class Demo04_ArrayCopy {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("添狗.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("添狗1.txt");
        //extracted(fileInputStream);
        //extracted(fileInputStream, fileOutputStream);

        byte[] arr = new byte[1024];//效率更快
        int len;
        //len = fileInputStream.read(arr);len代表的是被读取到的字节的个数
        //System.out.println("len = " + len);
        System.out.println("****************");
        long start = System.currentTimeMillis();

        while ((len = fileInputStream.read(arr)) != -1) {
            fileOutputStream.write(arr,0,len);
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start + "ms");

        fileInputStream.close();
        fileOutputStream.close();

    }

    private static void extracted(FileInputStream fileInputStream, FileOutputStream fileOutputStream) throws IOException {
        byte[] arr = new byte[2];
        int len;
        while ((len = fileInputStream.read(arr)) != -1) {
            fileOutputStream.write(arr, 0, len);
        }
    }

    private static void extracted(FileInputStream fileInputStream) throws IOException {
        byte[] arr = new byte[2];

        int a = fileInputStream.read(arr);
        System.out.println("a = " + a);
        for (byte b : arr) {
            System.out.println(b);
        }

        int b = fileInputStream.read(arr);
        System.out.println("b = " + b);
        for (byte b1 : arr) {
            System.out.println(b1);
        }
    }
}
